﻿//Nishanth

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Tzync
{
    /*****************************************************************************************************/
    /* Class corresponds to information of one File/Directory in the File System tree stored in logFile. */
    /*****************************************************************************************************/

    public class TreeNode
    {
        //Data Members of Class TreeNode
        private int id;
        private String fullPathHost;
        private String fullPathGuest;
        private String name;
        private Pair<int, int> versionHost;
        private long sizeHost;
        private Pair<int, int> versionGuest;
        private long sizeGuest;
        private String syncHost;
        private String syncGuest;
        private int parent;
        private List<int> childs;
        private Type type;
        public List<int> dynamicChildList;
        private bool isdeleted;

        

        /// <summary>
        /// Constructor to create an empty node.                    
        /// </summary>  
        public TreeNode()
        {
            versionHost = new Pair<int, int>();
            versionGuest = new Pair<int, int>();
            childs = new List<int>();
            dynamicChildList = new List<int>();
        }

        //Cant children be stored using a different function?
        // Needs to throw the right exceptions for invalid input.
        //Will change once things are converted to XML, so understanding the string input should be moved to a separate function as well.
        /// <summary> 
        /// Overloaded Constructor to initialise the values of the node from the string input        
        /// Post-Conditions: Node information is filled from the content of the string input.        
        /// </summary>
        /// <param name="input"></param>       
        public TreeNode(String input)
        {
            String[] arrWords, arrPair, arrAddress;
            arrWords = input.Split('|');

            //MetaDataEntryValidator(input);

            id = Convert.ToInt32(arrWords[0]);
            fullPathHost = arrWords[1];
            arrAddress = fullPathHost.Split('\\');
            name = arrAddress[arrAddress.Count() - 1];
            fullPathGuest = arrWords[2];
            if (name == "")
            {
                arrAddress = fullPathGuest.Split('\\');
                name = arrAddress[arrAddress.Count() - 1];
            }
            arrPair = arrWords[3].Split(',');
            versionHost = new Pair<int, int>(Convert.ToInt32(arrPair[0]), Convert.ToInt32(arrPair[1]));
            sizeHost = long.Parse(arrWords[4]);
            arrPair = arrWords[5].Split(',');
            versionGuest = new Pair<int, int>(Convert.ToInt32(arrPair[0]), Convert.ToInt32(arrPair[1]));
            sizeGuest = long.Parse(arrWords[6]);            
            syncHost = arrWords[7];
            syncGuest = arrWords[8];
            parent = Convert.ToInt32(arrWords[9]);

            //Storing the children if the Tree Node is a Directory
            //The list of Children is appended by -1 ALWAYS

            if (arrWords[10].Length > 0) //Folder
            {
                String[] arrChild = arrWords[10].Split(',');
                childs = new List<int>();
                dynamicChildList = new List<int>();
                arrChild = arrWords[10].Split(',');
                foreach (String s in arrChild)
                {
                    childs.Add(Convert.ToInt32(s));
                }
                type = Type.Folder;
            }
            else //File
            {
                childs = null;
                type = Type.File;
            }
        }


        /*********************************************************/
        /* Accessory Functions for the data members of the class */
        /*********************************************************/

        public int GetId()
        {
            return id;
        }

        public void SetId(int id)
        {
             this.id=id;
        }

        public String GetFullPathHost()
        {
            return fullPathHost;
        }

        public void SetFullPathHost(String path)
        {
            fullPathHost = path;
        }

        public String GetFullPathGuest()
        {
            return fullPathGuest;
        }

        public void SetFullPathGuest(String path)
        {
            fullPathGuest = path;
        }

        public String GetName()
        {
            return name;
        }

        public void SetName(String path)
        {
            name = path;
        }

        public Pair<int, int> GetVersionHost()
        {
            return versionHost;
        }

        public void SetVersionHost(Pair<int, int> p)
        {
            versionHost = p;
        }

        public long GetSizeHost()
        {
            return sizeHost;
        }

        public void SetSizeHost(long size)
        {
            sizeHost = size;
        }

        public Pair<int, int> GetVersionGuest()
        {
            return versionGuest;
        }

        public void SetVersionGuest(Pair<int, int> p)
        {
            versionGuest = p;
        }

        public long GetSizeGuest()
        {
            return sizeGuest;
        }

        public void SetSizeGuest(long size)
        {
            sizeGuest = size;
        }

        public String GetSyncHost()
        {
            return syncHost;
        }
        public void SetSyncHost(String timestamp)
        {
            syncHost = timestamp;
        }

        public String GetSyncGuest()
        {
            return syncGuest;
        }
        public void SetSyncGuest(String timestamp)
        {
            syncGuest = timestamp;
        }

        public int GetParent()
        {
            return parent;
        }
        public void SetParent(int p)
        {
            parent = p;
        }

        public List<int> GetChilds()
        {
            return childs;
        }

        public void SetChilds(List<int> list)
        {
            childs = list;
        }

        public Type GetNodeType()
        {
            return type;
        }

        public void SetNodeType(Type t)
        {
            type = t;
        }

        public void AddChild(int child)
        {
            dynamicChildList.Add(child);
        }

        public void PushChild(int c, int k)
        {
            childs.Insert(k, c);
        }

        public void RemoveChild(TreeNode child)
        {
            childs.Remove(child.GetId());
        }

        public bool Isdeleted
        {
            get { return isdeleted; }
            set { isdeleted = value; }
        }

        /**************************************************************/
        /* Accessory Functions for the data members of the class end !*/
        /**************************************************************/

        //Function to convert the Node information into String 

        public String toString()
        {
            String str = "";
            str = id + "|";
            str += fullPathHost + "|";
            str += fullPathGuest + "|";
            str += versionHost.First + "," + versionHost.Second + "|";
            str += sizeHost + "|";
            str += versionGuest.First + "," + versionGuest.Second + "|";
            str += sizeGuest + "|";
            str += syncHost + "|";
            str += syncGuest;
            str += "|";
            str += parent.ToString();
            str += "|";
            if (childs != null)
            {
                for (int s = 0; s < childs.Count(); s++)
                {
                    str += childs[s];
                    if (s != childs.Count() - 1) str += ',';
                }
            }
            str += "\n";
            return str;
        }

    }

    /*************************************************************************************************************************************/
    /* Class to store time stamp in our preferred way to avoid mis-interpretations due to different representations in different systems */
    /*************************************************************************************************************************************/

    public class DateTimeParser
    {
        private DateTime newDateTime;
        private String dateTime;

        //Constructor to initialise the local Date Time object                 
        //Post-Conditions: Local DateTime object is initialised with dt values in our preferred way.

        public DateTimeParser(DateTime dt)
        {
            DateTime date1 = dt;
            newDateTime = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);            
            dateTime = String.Format("{0:dd/MM/yyyy HH:mm:ss}", newDateTime);
        }

        public String GetNewDateTime()
        {
            return dateTime;
        }
    }

    /***********************/
    /* Class TreeNode ends */
    /***********************/
}
